package com.csu.cphone.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.csu.cphone.dao.MaintainPhoneMapper;
import com.csu.cphone.dao.MaintainProblemMapper;
import com.csu.cphone.dao.MaintainProblemSelectMapper;
import com.csu.cphone.dao.PhoneBrandMapper;
import com.csu.cphone.pojo.MaintainPhonePojo;
import com.csu.cphone.pojo.MaintainProblemPojo;
import com.csu.cphone.pojo.MaintainProblemSelectPojo;
import com.csu.cphone.pojo.PhoneBrandPojo;
import com.csu.cphone.service.MaintainService;
import com.csu.cphone.vo.MaintainPhoneTreeVo;

/**
 * @项目名称：cphone
 * @类名称：MaintainServiceImpl
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年10月27日 下午3:22:36
 * @company:
 */
@Service
public class MaintainServiceImpl implements MaintainService
{

    @Autowired
    private MaintainPhoneMapper maintainPhoneMapper;

    @Autowired
    private PhoneBrandMapper phoneBrandMapper;

    @Autowired
    private MaintainProblemMapper maintainProblemMapper;

    @Autowired
    private MaintainProblemSelectMapper maintainProblemSelectMapper;

    /**
     * @see com.csu.cphone.service.MaintainService#getMaintainProblemsByPhoneId(int)
     */
    @Override
    public List<MaintainProblemPojo> getMaintainProblemsByPhoneId(int phoneId)
    {

        List<MaintainProblemPojo> problems = maintainProblemMapper.getMaintainProblemsByPhoneId(phoneId);

        List<Integer> problemIds = new ArrayList<Integer>();
        for (MaintainProblemPojo problem : problems)
        {
            Integer problemId = problem.getId();
            problemIds.add(problemId);
        }

        List<MaintainProblemSelectPojo> selects = getSelectsByProblemIds(problemIds);

        Map<Integer, List<MaintainProblemSelectPojo>> selectMap = new HashMap<Integer, List<MaintainProblemSelectPojo>>();
        for (MaintainProblemSelectPojo select : selects)
        {
            Integer problemId = select.getProblemId();
            List<MaintainProblemSelectPojo> thisProblemSelects = selectMap.get(problemId);
            if (thisProblemSelects == null)
            {
                thisProblemSelects = new ArrayList<MaintainProblemSelectPojo>();
            }
            thisProblemSelects.add(select);
            selectMap.put(problemId, thisProblemSelects);
        }

        for (MaintainProblemPojo problem : problems)
        {
            Integer problemId = problem.getId();
            problem.setSelects(selectMap.get(problemId));
        }

        return problems;
    }

    
    /** @description
     * @author TangC
     * @date 2017年12月11日 上午12:25:19
     * @param problemIds
     * @return
     */
    
    private List<MaintainProblemSelectPojo> getSelectsByProblemIds(List<Integer> problemIds)
    {
        if (CollectionUtils.isEmpty(problemIds))
        {
            return new ArrayList<MaintainProblemSelectPojo>();
        }
        return maintainProblemSelectMapper.selectByProblemIds(problemIds);
    }

    /**
     * @see com.csu.cphone.service.MaintainService#getMaintainPhoneTreeList()
     */
    @Override
    public List<MaintainPhoneTreeVo> getMaintainPhoneTreeList()
    {
        List<MaintainPhoneTreeVo> maintainPhoneTreeList = new ArrayList<MaintainPhoneTreeVo>();

        List<PhoneBrandPojo> brands = phoneBrandMapper.selectBrandList();

        for (PhoneBrandPojo brand : brands)
        {
            Integer brandId = brand.getId();
            List<MaintainPhonePojo> maintainPhones = maintainPhoneMapper.selectMaintainPhoneList(brandId);

            MaintainPhoneTreeVo maintainPhoneTree = new MaintainPhoneTreeVo();
            maintainPhoneTree.setBrandId(brandId);
            maintainPhoneTree.setBrandName(brand.getBrandName());
            maintainPhoneTree.setMaintainPhones(maintainPhones);

            maintainPhoneTreeList.add(maintainPhoneTree);
        }

        return maintainPhoneTreeList;
    }

    /**
     * @see com.csu.cphone.service.MaintainService#addPhone(int,
     *      java.lang.String, java.lang.String)
     */
    @Override
    public MaintainPhonePojo addPhone(int brandId, String name, String url)
    {
        MaintainPhonePojo phone = new MaintainPhonePojo();
        phone.setBrandId(brandId);
        phone.setName(name);
        phone.setPicUrl(url);
        phone.setStatus(1);
        maintainPhoneMapper.insert(phone);
        return phone;
    }

    /**
     * @see com.csu.cphone.service.MaintainService#addMaintainProblem(com.csu.cphone.pojo.MaintainProblemPojo)
     */
    @Override
    public MaintainProblemPojo addMaintainProblem(MaintainProblemPojo phoneProblem)
    {
        List<MaintainProblemSelectPojo> selects = phoneProblem.getSelects();

        maintainProblemMapper.insert(phoneProblem);
        for (MaintainProblemSelectPojo select : selects)
        {
            select.setProblemId(phoneProblem.getId());
        }
        maintainProblemSelectMapper.batchInsert(selects);
        return phoneProblem;
    }

    /**
     * @see com.csu.cphone.service.MaintainService#offer(java.util.List)
     */
    @Override
    public Double offer(List<MaintainProblemSelectPojo> problemSelects)
    {
        Double totalPrice = 0d;
        for (MaintainProblemSelectPojo selectPojo : problemSelects)
        {
            double discount = selectPojo.getCost();
            totalPrice = totalPrice + discount;
        }
        return totalPrice;
    }
}
